package org.devops

def GetCode(){
    checkout scmGit(branches: [[name: "${env.branchName}"]], 
                    extensions: [], 
                    userRemoteConfigs: [[url: "${env.srcUrl}"]])
    sh "ls -l "
}


def Build(jdks,buildDir,buildShell,projectType){
    if (projectType == "backend"){
    //下载settings.xml
        settings = libraryResource 'settings.xml'
        writeFile file: 'settings.xml', text: settings

        sh """
            export JAVA_HOME=${jdks["${params.jdkVersion}"]}
            echo \$JAVA_HOME
            export PATH=\$JAVA_HOME/bin:\$PATH
            java -version
            ls -l 
            
            #mvn clean package -pl  ${env.serviceName} -am  -s settings.xml
            ${buildShell} -Pcloud
            ls -l ${buildDir}/target/
        """
    }
    if (projectType == "web"){
        sh """
            ${buildShell}
            ls -l dist/
            tar zcf ${env.serviceName}.tar.gz dist/*
            ls -l ${buildDir}
            
        """
    }
}

def Build1(jdks,buildDir,buildShell,projectType){
    if (projectType == "backend"){
        //下载settings.xml
        settings = libraryResource 'settings.xml'
        writeFile file: 'settings.xml', text: settings
        sh """
            export JAVA_HOME=${jdks["${params.JdkVersion}"]}
            echo \$JAVA_HOME
            export PATH=\$JAVA_HOME/bin:\$PATH
            java -version
            ls -l 
            #mvn clean package -pl  ${env.serviceName} -am  -s settings.xml
            ${buildShell}
            ls -l ${buildDir}/target/
        """
    } 

    if (projectType == "web"){
        sh """
            ${buildShell}
            ls -l ${buildDir}
            tar -czvf ${env.serviceName}.tar.gz dist/*
            ls -l dist/
            
        """
    }
}

def Build2(jdks){
    //下载settings.xml
    settings = libraryResource 'settings.xml'
    writeFile file: 'settings.xml', text: settings
    if ("${env.serviceName}" == "pig-upms"){
        sh """
        export JAVA_HOME=${jdks["${params.JdkVersion}"]}
        echo \$JAVA_HOME
        export PATH=\$JAVA_HOME/bin:\$PATH
        java -version
        ls -l 
        mvn clean package -s settings.xml
        #mvn clean package -pl pig-upms-biz -am -f ${env.serviceName}/pom.xml -s settings.xml  
        ls -l ${env.serviceName}/pig-upms-biz/target/
    """
    } else{
        sh """
                export JAVA_HOME=${jdks["${params.JdkVersion}"]}
                echo \$JAVA_HOME
                export PATH=\$JAVA_HOME/bin:\$PATH
                java -version
                ls -l 
                mvn clean package -pl  ${env.serviceName} -am  -s settings.xml
                ls -l ${env.serviceName}/target/
            """
    }
}

def PushArtifactsWithWeb(repoName,buildDir,artifactName){
    withCredentials([usernamePassword(credentialsId: '1a3364f2-dfbf-4ed0-9359-06f55997eb51', passwordVariable: 'NEXUSUSER', usernameVariable: 'NEXUSPASS')]) {
        sh """
            curl -X POST "http://nexus.idevops.site/service/rest/v1/components?repository=${repoName}" \
            -H "accept: application/json" \
            -H "Content-Type: multipart/form-data" \
            -F "raw.directory=${buildDir}" \
            -F "raw.asset1=@${artifactName};type=application/x-gzip" \
            -F "raw.asset1.filename=${artifactName}" \
            -u ${NEXUSUSER}:${NEXUSPASS}
        """
    }
}


def PushArtifacts(repoName,buildDir,artifactName){
    sh """
        mvn deploy:deploy-file \
        -s settings.xml \
        -DgroupId=com.pig \
        -DartifactId=${env.serviceName} \
        -Dversion=${env.branchName} \
        -Dpackaging=jar \
        -Dfile=${buildDir}/target/${artifactName}.jar \
        -Durl=http://nexus.idevops.site/repository/${repoName}/  \
        -DrepositoryId=pig
    """
}


def BuildDockerImages(dockerfileDir, dockerContextDir){
    repoName = "devops-lc"
    
    // Get config.json
    data = libraryResource 'config.json'
    //println(data)
    writeFile file: '/kaniko/.docker/config.json', text: data

    //Build Docker images
    env.dockerImageName = "ccr.ccs.tencentyun.com/${repoName}/${env.serviceName}:${env.branchName}"
    sh """
        export DOCKER_CONFIG="/kaniko/.docker/" 
        ls -l /kaniko/.docker/config.json
        /kaniko/executor  \
        --dockerfile=${dockerfileDir}/Dockerfile \
        --context=${dockerContextDir} \
        --destination=${env.dockerImageName}
    """
}